package com.cskaoyan.order.utils;

import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.order.constant.OrderRetCode;

import java.util.List;
import java.util.Objects;

/**
 * @author Chen_Feng
 * @since 2022/07/09 00:30
 */

public class ValidateUtil {

    /**
     * 如果参数是null，或者字符串为空，则抛出异常
     *
     * @param params 要校验的参数
     * @return void
     * @author Chen_Feng
     * @since 2022/07/09 0:37
     */
    public static void validate(Object... params) {
        for (Object o : params) {
            if (o == null) {
                throw new ValidateException(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(), OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
            }
            if (o instanceof String) {
                String s = (String) o;
                if (Objects.equals("", s.trim())) {
                    throw new ValidateException(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(), OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
                }
            }
            if (o instanceof List) {
                if (((List) o).isEmpty()) {
                    throw new ValidateException(OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getCode(), OrderRetCode.REQUISITE_PARAMETER_NOT_EXIST.getMessage());
                }
            }
        }
    }
}
